(*
  Copyright (c) 2009 Barry Schwartz

  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
*)

open UTypes
open SymbolTable
open VMTypes
  
type scope =
  { symbol_table : (uc_string, Lexer.token_class) Hashtbl.t;
    global_symbols : (int, unknown) Hashtbl.t;
    local_symbols : (int, (int * int)) Hashtbl.t; depth : int; shift : int
  }

let create () =
  {
    symbol_table = Lexer.initial_symbol_table ();
    global_symbols = Hashtbl.create 1000;
    local_symbols = Hashtbl.create 100;
    depth = 0;
    shift = 0;
  }
  
let symbol_table scope = scope.symbol_table
  
let add_bin_op scope pri assoc sym =
  let str = symbol_to_string sym
  in Hashtbl.add scope.symbol_table str (Lexer.BINOP (sym, pri, assoc))
  
let add_pre_op scope sym =
  let str = symbol_to_string sym
  in Hashtbl.add scope.symbol_table str (Lexer.PREOP sym)
  
let add_post_op scope sym =
  let str = symbol_to_string sym
  in Hashtbl.add scope.symbol_table str (Lexer.POSTOP sym)
  
let copy scope =
  {
    symbol_table = Hashtbl.copy scope.symbol_table;
    global_symbols = Hashtbl.copy scope.global_symbols;
    local_symbols = Hashtbl.copy scope.local_symbols;
    depth = scope.depth;
    shift = scope.shift;
  }
  
let add_global scope symbol contents =
  Hashtbl.replace scope.global_symbols symbol (create_unknown contents)
  
let push scope symbols =
  let local = Hashtbl.copy scope.local_symbols in
  let depth = scope.depth + 1 in
  let rec iter n symbols =
    match symbols with
    | [] -> ({ (scope) with local_symbols = local; depth = depth; }, n)
    | s :: ss -> (Hashtbl.replace local s (depth, n); iter (n + 1) ss)
  in iter 0 symbols
  
let shift scope off = { (scope) with shift = scope.shift + off; }
  
let lookup_local scope symbol =
  let (level, index) = Hashtbl.find scope.local_symbols symbol
  in (((scope.depth - level) + scope.shift), index)
  
let lookup_global scope symbol = Hashtbl.find scope.global_symbols symbol
  
let lookup scope symbol =
  try
    let (depth, index) = lookup_local scope symbol
    in BVariable (depth, index)
  with | Not_found -> BGlobal (lookup_global scope symbol)
  

